APISIX 如何扮演 Load Balancer 的角色。
建立具有兩個上游服務的路由。
curl -i "http://127.0.0.1:9180/apisix/admin/routes" -X PUT -d '
{
"id": "getting-started-headers",
"uri": "/headers",
"upstream" : {
"type": "roundrobin",
"nodes": {
"httpbin.org:443": 1,
"mock.api7.ai:443": 1
},
"pass_host": "node",
"scheme": "https"
}
}'
成功建立Route回傳HTTP/1.1 201 Created
。
如何驗證負載平衡的結果?
上游服務回傳不同資料回來。
產生100次Requests來測試負載平衡的效果。
hc=$(seq 100 | xargs -I {} curl "http://127.0.0.1:9080/headers" -sL | grep "httpbin" | wc -l);
echo httpbin.org: $hc,
mock.api7.ai: $((100 - $hc))
結果顯示:
httpbin.org: 51, mock.api7.ai: 49
Requests打到上游服務的次數接近相等。
When type is roundrobin
and weight
is not equal,
curl -i "http://127.0.0.1:9180/apisix/admin/routes" -X PUT -d '
{
"id": "getting-started-headers",
"uri": "/headers",
"upstream" : {
"type": "roundrobin",
"nodes": {
"httpbin.org:443": 1,
"mock.api7.ai:443": 2
},
"pass_host": "node",
"scheme": "https"
}
}'
What is the result ?